package com.ruoyi.appversion.controller;

import com.ruoyi.appversion.domain.VersionEntity;
import com.ruoyi.appversion.service.VersionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 用于安卓平板的apk版本更新
 */
@Api("安卓平板app版本控制类")
@Slf4j
@RestController
@RequestMapping("/app")
public class AppVersionController extends BaseController {

    @Autowired
    VersionService versionService;

    /**
     * 上传apk
     * @param versionNum
     * @param file
     * @param devOrMaster
     * @return
     */
    @ApiOperation("上传apk")
    @PostMapping("/upload")
    public AjaxResult uploadApk(String versionNum, @RequestParam("file") MultipartFile file, String devOrMaster){
        if (Pattern.matches("^[0-9].[0-9].[0-9]", versionNum)){
            if (devOrMaster.matches("dev") || devOrMaster.matches("master")){
                try {
                    VersionEntity version = versionService.uploadApk(versionNum, file, devOrMaster);
                    if (version == null){
                        return AjaxResult.success("this version exists!");
                    }else {
                        return AjaxResult.success(version);
                    }
                } catch (IOException e){
                    log.error(e.getMessage());
                    return AjaxResult.error("process error, please check you inputs");
                }
            } else {
                return AjaxResult.error("branch error, please input dev or master");
            }
        } else {
            return AjaxResult.error("version number error, please input like 0.1.2");
        }
    }

    /**
     * 检查安卓平板更新
     * @param versionNum
     * @return
     */
    @ApiOperation("检查更新，若有新版本则返回下载地址")
    @RequestMapping(value = "/check4update", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult versionCheck(String versionNum){
        if (Pattern.matches("^[0-9].[0-9].[0-9]", versionNum)){
            try {
                String check = versionService.versionCheck(versionNum);
                if (!Objects.equals(check, "null")){
                    String apkPath = versionService.getApkPath(check);
                    return AjaxResult.success(apkPath).put("update", "true");
                } else {
                    return AjaxResult.success("latest!").put("update", "false");
                }
            } catch (Exception e){
                log.error(e.getMessage());
                return AjaxResult.error("please check the version number");
            }
        } else {
            return AjaxResult.error("version number error, please input like 0.1.2");
        }
    }
}
